package io.github.wslxm.springbootplus2.starter.captcha.config;

import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
import cloud.tianai.captcha.resource.ResourceStore;
import cloud.tianai.captcha.resource.common.model.dto.Resource;
import io.github.wslxm.springbootplus2.starter.captcha.util.CaptchaResourceUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

@Component
@RequiredArgsConstructor
public class CaptchaResourceConfiguration {

    private final ResourceStore resourceStore;

    @PostConstruct
    public void init() {
        List<String> captchaImages = CaptchaResourceUtil.getFileNamesByClasspathDirectory("bgimages");
        // 添加自定义背景图片
        String[] captchaTypes = {CaptchaTypeConstant.SLIDER, CaptchaTypeConstant.ROTATE, CaptchaTypeConstant.CONCAT, CaptchaTypeConstant.WORD_IMAGE_CLICK};
        for (int i = 0; i < captchaTypes.length; i++) {
            String captchaType = captchaTypes[i];
            for (String captchaImage : captchaImages) {
                resourceStore.addResource(captchaType, new Resource("classpath", captchaImage, "default"));
            }
        }
    }
}

